package com.sanpu.kxe.business.repay.presenter;

import com.alibaba.fastjson.JSONObject;
import com.google.gson.Gson;
import com.sanpu.kxe.business.card.bean.BankInfoOutput;
import com.sanpu.kxe.business.card.provider.ICardProvider;
import com.sanpu.kxe.business.repay.contract.AddCreditContract;
import com.sanpu.kxe.business.trade.provider.ITradeProvider;
import com.sanpu.kxe.common.interfaces.CommonConfig;
import com.sanpu.kxe.common.interfaces.UrlConstants;
import com.sanpu.kxe.rxservice.RequestMap;
import com.sanpu.kxe.utils.PublicProviderUtil;

import java.util.List;

import rx.Observer;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
import rx.subscriptions.CompositeSubscription;

/**
 * Description：添加信用卡
 * Created by 薛勇军 on 2019/4/10.
 */

public class AddCreditPresenter implements AddCreditContract.Presenter {

    private AddCreditContract.View mView;

    private CompositeSubscription mSubscription;

    private String apprtId;

    public AddCreditPresenter(AddCreditContract.View mView, String apprtId) {
        this.mView = mView;
        this.apprtId = apprtId;
        mSubscription = new CompositeSubscription();
    }

    @Override
    public void onCommmit(String bankId, String billDay, String refundDay) {
        JSONObject reqData = new JSONObject();
        reqData.put("bankId", bankId);
        reqData.put("apprtId", apprtId);
        reqData.put("billDay", billDay);
        reqData.put("dueDay", refundDay);
        RequestMap requestMap = new RequestMap();
        requestMap.put(CommonConfig.REQDATA, new Gson().toJson(reqData));
        ITradeProvider provider = PublicProviderUtil.getProvider(ITradeProvider.class);
        if (provider != null) {
            provider.openQuickPayWebViewActivity(mView.getContext(),
                    UrlConstants.URL_BINDCARD, requestMap.map2ReqStr().replace("+", "%2B"));
        }
    }

    @Override
    public void selectUrmBankPage() {
        final ICardProvider provider = PublicProviderUtil.getProvider(ICardProvider.class);
        if (provider != null) {
            Subscription subscription = provider.selectUrmBankPage(CommonConfig.CREDIT_CARD_TYPE)
                    .subscribeOn(Schedulers.io())
                    .unsubscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new Observer<List<BankInfoOutput>>() {
                        @Override
                        public void onCompleted() {

                        }

                        @Override
                        public void onError(Throwable e) {
                            mView.showErrorText(e);
                        }

                        @Override
                        public void onNext(List<BankInfoOutput> bankInfoOutputs) {
                            if (bankInfoOutputs == null || bankInfoOutputs.size() == 0) {
                                provider.openAddBankSetupActivity(mView.getContext(), true);
                                return;
                            }
                            mView.showDataView(bankInfoOutputs);
                        }
                    });
            mSubscription.add(subscription);
        }
    }

    @Override
    public void onDestroyPresenter() {
        if (mSubscription != null && !mSubscription.isUnsubscribed()) {
            mSubscription.unsubscribe();
            mSubscription = null;
        }
        if (mView != null) {
            mView = null;
        }
    }
}
